import 'dart:io';

import 'package:camera/camera.dart';
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;

import 'action.dart';
import 'state.dart';

Widget buildView(
    CameraState state, Dispatch dispatch, ViewService viewService) {
  List<Widget> photoPreview() {
    var views = List<Widget>();
    if (state.selected.length > 0) {
      views.add(
        Positioned(
          top: 3,
          right: 3,
          child: Container(
            padding: EdgeInsets.only(left: 4, right: 4),
            decoration: const BoxDecoration(
                gradient: LinearGradient(
                  colors: <Color>[
                    Colors.blue,
                    Colors.blue,
                  ],
                ),
                borderRadius: BorderRadius.all(Radius.circular(80.0))),
            child: Text(state.selected.length.toString(),
                style: TextStyle(color: Colors.white)),
          ),
        ),
      );
    }
    return views;
  }

  List<Widget> getViews() {
    var views = List<Widget>();
    if (state.controller != null) {
      views.add(AspectRatio(
          aspectRatio: state.controller.value.aspectRatio,
          child: CameraPreview(state.controller)));
    }

    views.add(Positioned(
      left: 0,
      right: 0,
      bottom: 32,
      child: Stack(
        children: <Widget>[
//          IconButton(
//            iconSize: 50,
//            icon: Image.asset("assets/images/default_gallery.png"),
//            onPressed: () => dispatch(CameraActionCreator.onGallery()),
//          ),
          Center(
            child: Container(
              width: 56,
              height: 56,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Colors.blue,
              ),
              padding: EdgeInsets.all(3.0),
              child: IconButton(
                iconSize: 30,
                icon: Image.asset("assets/images/camera3.png"),
                onPressed: () => dispatch(CameraActionCreator.onTake()),
              ),
            ),
          ),
          Positioned(
            right: 10,
            child: FlatButton(
              textColor: Colors.white,
              padding: const EdgeInsets.all(0.0),
              child: Container(
                decoration: const BoxDecoration(
                    gradient: LinearGradient(
                      colors: <Color>[
                        Colors.blue,
                        Colors.blueAccent,
                      ],
                    ),
                    borderRadius: BorderRadius.all(Radius.circular(80.0))),
                padding: const EdgeInsets.fromLTRB(24, 10, 24, 10),
                child:
                    Text('选照片 >', style: TextStyle(color: Colors.white)),
              ),
              onPressed: () => dispatch(CameraActionCreator.onGallery()),
            ),
          ),
        ],
      ),
    ));

    if (state.tmpPhoto != null) {
      views.add(Image.file(File(state.tmpPhoto), fit: BoxFit.fill));
      views.add(Positioned(
        left: 0,
        right: 0,
        bottom: 16,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Container(
              width: 60,
              height: 60,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Colors.grey,
              ),
              padding: EdgeInsets.all(10.0),
              child: IconButton(
                iconSize: 30,
                icon: Image.asset("assets/images/cancel.png"),
                onPressed: () => dispatch(CameraActionCreator.reject()),
              ),
            ),
            Container(
              width: 60,
              height: 60,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Colors.white,
              ),
              padding: EdgeInsets.all(6.0),
              child: IconButton(
                iconSize: 30,
                icon: Image.asset("assets/images/ok.png"),
                onPressed: () => dispatch(CameraActionCreator.onSave()),
              ),
            ),
          ],
        ),
      ));
    }

    return views;
  }

  return new Scaffold(
      appBar: new AppBar(
          centerTitle: true,
          elevation: 2.0,
          title: new Text(
            "拍照",
            style: TextStyle(color: Colors.black54, fontSize: 18),
          )),
      body: Stack(
        fit: StackFit.expand,
        children: getViews(),
      ));
}
